package com.buba.customer.controller;

import com.buba.customer.pojo.CustomerInf;
import com.buba.customer.pojo.CustomerLevelInf;
import com.buba.customer.pojo.CustomerLogin;
import com.buba.customer.service.CustomerInfService;
import com.buba.customer.service.CustomerLevelInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户详细信息控制层
 *
 */
@Controller
@RequestMapping("/CustomerInf")
public class CustomerInfController {

    private CustomerLevelInfService customerLevelInfService;
    private CustomerInfService customerInfService;


    /**
     * 删除用户详细信息
     * @param ids
     * @return
     */
    @RequestMapping("/delCustomerInf.do")
    @ResponseBody  
    public Map<String,Object> delCustomerInf(String ids){
        Map<String,Object> map = new HashMap<>();
        String[] split = ids.split(",");
        Integer count = customerInfService.delCustomerInf(split);
        map.put("msg","删除了"+count+"条数据");

        return map;
    }
    /**
     * 查询详细用户信息
     * @param
     * @param
     * @return
     */
    @RequestMapping("/selCustomerInf.do")
    @ResponseBody
    public Map<String,Object> selCustomerInf(Integer page,Integer limit){
        Map<String,Object> map = new HashMap<>();

        //查询总条数，查询当前页的数据
        Long count = customerInfService.selCount(map);
        //把分页参数携带过去
        map.put("page",(page-1) * limit);
        map.put("limit",limit);



        List<CustomerInf> list = customerInfService.selCustomerInf(map);
        list.forEach(System.out::println);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",list);
        return map;
    }
    /**
     * 添加用户详细信息方法
     * @param customerInf
     * @return
     */
    @RequestMapping("/addCustomerInf.do")
    @ResponseBody
    public Map<String, Object> addCustomerInf(CustomerInf customerInf) {
        // 日期格式化类 把日期转换成想要的格式 new Date() 当前日期
        // 2020-12-8
        DateFormat bf = new SimpleDateFormat("yyyy-MM-dd");
        String format = bf.format(new Date());
        customerInf.setRegister_time(format);
        // 关于会员级别 需要根据用户的积分自动指定会员级别
        // 关于会员级别的第一个业务 根据会员基本查询会员对象
        CustomerLevelInf customerLevelInf = customerLevelInfService.selCustomerLevelInfByUserPoint(customerInf.getUser_point());
        // 把查询出来的会员级别对象装入用户详细信息对象中
        customerInf.setCustomerLevelInf(customerLevelInf);

        Map<String, Object> map = new HashMap<>();
        //把调用业务层查询账号是否被注册
        Integer exists = customerInfService.selCustomerInfByName(customerInf);
        if(exists > 0){
            map.put("msg","该账号已经被注册");
            return map;
        }

        //把要添加的信息传入业务层
        Integer count = customerInfService.addCustomerInf(customerInf);
        map.put("msg", "添加了" + count + "数据");

        return map;
    }





    //get、set   ao tou ruai de
    public CustomerLevelInfService getCustomerLevelInfService() {
        return customerLevelInfService;
    }

    @Autowired
    public void setCustomerLevelInfService(CustomerLevelInfService customerLevelInfService) {
        this.customerLevelInfService = customerLevelInfService;
    }

    public CustomerInfService getCustomerInfService() {
        return customerInfService;
    }

    @Autowired
    public void setCustomerInfService(CustomerInfService customerInfService) {
        this.customerInfService = customerInfService;
    }
}